package com.liyongjian.apidoc.mvc;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.liyongjian.apidoc.dto.BaseProperties;
import com.liyongjian.apidoc.dto.SessionUser;
import com.liyongjian.apidoc.utils.JsonMapper;

public class BaseController extends BaseProperties {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    protected JsonMapper jsonMapper = new JsonMapper();


    protected String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }



    protected void saveCurrentUser(HttpServletRequest request, SessionUser su) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", su.getId());
        map.put("number", su.getNumber());
        map.put("name", su.getName());
        map.put("phone", su.getPhone());
        map.put("userOrganizaId", su.getUserOrganizaId());
        request.getSession().setAttribute("user", map);
    }

    protected SessionUser getCurrentUser(HttpServletRequest request) {
        Map<String, Object> map = (Map<String, Object>) request.getSession().getAttribute("user");
        if (map == null) {
            throw new RuntimeException("未登陆");
        }
        SessionUser user = new SessionUser();
        user.setId((Long) map.get("id"));
        user.setNumber((Long) map.get("number"));
        user.setName((String) map.get("name"));
        user.setPhone((String) map.get("phone"));
        return user;
    }

    protected void logout(HttpServletRequest request) {
        request.getSession().removeAttribute("user");
    }

    public Map<String, Object> getRequestParameter(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        Enumeration<String> en = request.getParameterNames();
        while (en.hasMoreElements()) {
            String key = (String) en.nextElement();
            String value = request.getParameter(key);
            map.put(key, value);
        }
        return map;
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    public Object getRequestParameter(HttpServletRequest request, Class c) {
        Map<String, Object> map = new HashMap<String, Object>();
        Enumeration<String> en = request.getParameterNames();
        while (en.hasMoreElements()) {
            String key = (String) en.nextElement();
            String value = request.getParameter(key);
            map.put(key, value);
        }
        return jsonMapper.fromJson(jsonMapper.toJson(map), c);
    }

}
